import {Component, ViewChild} from '@angular/core';
import {Content, IonicPage, NavController, NavParams} from 'ionic-angular';
import {HttpService} from "../../providers/HttpService";

/**
 * Generated class for the NewsPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-news',
  templateUrl: 'news.html',
})
export class NewsPage {

    @ViewChild(Content) content: Content;
    start: number = 0;
    express_list: any[] = [];

    constructor(public navCtrl: NavController, public navParams: NavParams,
                private httpService: HttpService) {
    }

    ionViewDidLoad() {

    }

    ionViewWillEnter() {
        this.getNewsExpress(100).then(res => {
            if (res.status == 0) {
                this.express_list = res.data.result;
            }
        });
    }

    getItemStarStyle(isShow: boolean) {
        return isShow ? 'ios-star' : 'ios-star-outline';
    }

    getItemPriceStyle(type: string) {
        return {
            'none-tag': type == '0',
            'up-tag': type == '1',
            'down-tag': type == '2'
        };
    }

    getNewsExpress(size: number, init: boolean = true): Promise<any> {
        return new Promise((resolve, reject) => {
            this.httpService.instance()
                .setUrl('/gold/getNewsExpress')
                .setQuery({
                    size: size
                })
                .showLoader(init)
                .getRequest()
                .then(res => {
                    resolve(res);
                });
        });
    }

    onRefresh(refresher) {
        this.start = 0;
        this.getNewsExpress(100, false).then(res => {
            if (res.status == 0) {
                this.express_list = res.data.result;
            }
            refresher.complete();
        });
    }

    /*onInfinite(infiniteScroll) {
        console.log('onInfinite: ', infiniteScroll);
        this.start += 10;
        this.getNewsList(this.start, 10).then(res => {
            if (this.news_type == 1) {
                this.news_list.push(res);
            } else if (this.news_type == 2) {
                this.data_list.push(res);
            }
            infiniteScroll.complete();
        });
    }*/

    onFinanceCalendar() {
        this.navCtrl.push('FinanceCalendarPage');
    }

    onScrollTop() {
        this.content.scrollToTop();
    }
}
